/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJW
 * Date: 2024-07-27
 * Time: 20:10
 */
public class HashBuck2<K,V> {
    static class Node<K,V>{
        public K key;
        public V val;

        public Node<K,V> next;

        public Node(K key,V val){
            this.key = key;
            this.val = val;
        }
    }

    public Node<K,V>[] array;
    public int usedSize;
    public static final float DEAFULT_LOAD_FACTOR = 0.75f;

    public HashBuck2(){
        array = (Node<K, V>[]) new Node[10];
    }

    public void put(K key,V val){
        int hash = key.hashCode();
        int index = hash % array.length;
        //遍历index下标的链表 是否存在key 存在更新value 不存在进行头插法 插入数据
        Node<K,V> cur = array[index];
        while(cur != null){
            if(cur.equals(key)){
                //更新value
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //cur == null 链表遍历完成没有找到这个key
        Node<K,V> node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        usedSize++;//8
    }

    public V getValue(K key){
        int hash = key.hashCode();
        int index = hash % array.length;
        Node<K,V> cur = array[index];
        while(cur != null){
            if(cur.key.equals(key)){
                //更新value
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }
}
